using Newtonsoft.Json;
using System;
using UnityEngine;

public class MeihuayiZhanStockResult
{
    public float right;//1 正确，0 错误
    public float predictJi;//1 预测为吉，0 预测为凶，
    public float predictXiong;//1 预测为凶，0 预测为吉，
}
public class StockMeihuayiCtrl
{
    public const int openHour = 9;
    public const int openMinute = 30;
    public const int closeHour = 9;
    public const int closeMinute = 30;
    
    /// <summary>
    /// 开盘价和收盘价都预测
    /// </summary>
    /// <param name="stockCode"></param>
    /// <param name="transtion"></param>
    /// <param name="transitionLastDay"></param>
    /// <returns></returns>
    public static MeihuayiZhanStockResult ZhanWithStockTransition1(string stockCode, TransitionData transtion, TransitionData transitionLastDay)
    {
        var date = DateTime.Parse(transtion.d);
        // Debug.Log("date:"+date);
        MeihuayiResult openPredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        MeihuayiResult closePredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        string msg = $"当前交易数据：{JsonConvert.SerializeObject(transtion)}\n"
                     + $"前一个交易日的数据：{JsonConvert.SerializeObject(transitionLastDay)}\n"
                     + $"开盘预测：{JsonConvert.SerializeObject(openPredict)}\n"
                     + $"收盘预测：{JsonConvert.SerializeObject(closePredict)}";
        Debug.Log(msg);
        MeihuayiZhanStockResult result = new MeihuayiZhanStockResult();

        //开盘是否涨
        bool openUp = transtion.o >= transitionLastDay.c;
        //收盘是否涨
        bool closeUp = transtion.c >= transitionLastDay.c;

        if (openPredict.zhuGua.jixiong == JiXiong.大吉 || openPredict.zhuGua.jixiong == JiXiong.小吉)
        {
            result.predictJi+=0.5f;
            if(openUp)
            { result.right += 0.5f; }//Debug.Log("开盘预测涨 正确");}
        }
        else
        {
            if(!openUp)
            { result.right += 0.5f;}//Debug.Log("开盘预测跌 正确");}
        }
        if (closePredict.bianGua.jixiong == JiXiong.大吉 || closePredict.bianGua.jixiong == JiXiong.小吉)
        {
            result.predictJi+=0.5f;
            if(closeUp)
            {result.right += 0.5f;}//Debug.Log("收盘预测涨 正确");}
        }
        else
        {
            if(!closeUp)
            { result.right += 0.5f;}//Debug.Log("收盘预测跌 正确");}
        }
        return result;
    }
    /// <summary>
    /// 仅预测收盘价
    /// </summary>
    /// <param name="stockCode"></param>
    /// <param name="transtion"></param>
    /// <param name="transitionLastDay"></param>
    /// <returns></returns>
    public static MeihuayiZhanStockResult ZhanWithStockTransition2(string stockCode, TransitionData transtion, TransitionData transitionLastDay)
    {
        var date = DateTime.Parse(transtion.d);
        MeihuayiResult openPredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        MeihuayiResult closePredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        string msg = $"当前交易数据：{JsonConvert.SerializeObject(transtion)}\n"
                     + $"前一个交易日的数据：{JsonConvert.SerializeObject(transitionLastDay)}\n"
                     + $"开盘预测：{JsonConvert.SerializeObject(openPredict)}\n"
                     + $"收盘预测：{JsonConvert.SerializeObject(closePredict)}";
        Debug.Log(msg);
        MeihuayiZhanStockResult result = new MeihuayiZhanStockResult();

        //开盘是否涨
        bool openUp = transtion.o >= transitionLastDay.c;
        //收盘是否涨
        bool closeUp = transtion.c >= transitionLastDay.c;

        // if (openPredict.zhuGua.jixiong == JiXiong.大吉 || openPredict.zhuGua.jixiong == JiXiong.小吉)
        // {
        //     if(openUp)
        //     { result.right += 0.5f; }//Debug.Log("开盘预测涨 正确");}
        // }
        // else
        // {
        //     if(!openUp)
        //     { result.right += 0.5f;}//Debug.Log("开盘预测跌 正确");}
        // }
        if (closePredict.bianGua.jixiong == JiXiong.大吉 || closePredict.bianGua.jixiong == JiXiong.小吉)
        {
            result.predictJi=1f;
            if(closeUp)
            {result.right = 1f;}//Debug.Log("收盘预测涨 正确");}
        }
        else
        {
            if(!closeUp)
            { result.right = 1f;}//Debug.Log("收盘预测跌 正确");}
        }
        return result;
    }
    
    /// <summary>
    /// 仅预测收盘价和吉的情况
    /// </summary>
    /// <param name="stockCode"></param>
    /// <param name="transtion"></param>
    /// <param name="transitionLastDay"></param>
    /// <returns></returns>
    public static MeihuayiZhanStockResult ZhanWithStockTransition3(string stockCode, TransitionData transtion, TransitionData transitionLastDay)
    {
        var date = DateTime.Parse(transtion.d);
        MeihuayiResult openPredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        MeihuayiResult closePredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);

        MeihuayiZhanStockResult result = new MeihuayiZhanStockResult();

        //开盘是否涨
        bool openUp = transtion.o >= transitionLastDay.c;
        //收盘是否涨
        bool closeUp = transtion.c >= transitionLastDay.c;

        bool zhuGuaJi = closePredict.zhuGua.jixiong == JiXiong.大吉 ;//|| closePredict.zhuGua.jixiong == JiXiong.小吉;
        bool bianGuaJi = closePredict.bianGua.jixiong == JiXiong.大吉 ;//|| closePredict.bianGua.jixiong == JiXiong.小吉;
        if (zhuGuaJi && bianGuaJi)
        {
            result.predictJi = 1f;
            if(closeUp)
            {result.right = 1f;}//Debug.Log("收盘预测涨 正确");}
        }

        // string msg = $"当前交易数据：{JsonConvert.SerializeObject(transtion)}\n"
        //              + $"前一个交易日的数据：{JsonConvert.SerializeObject(transitionLastDay)}\n"
        //              + $"开盘预测：{JsonConvert.SerializeObject(openPredict)}\n"
        //              + $"收盘预测：{JsonConvert.SerializeObject(closePredict)}\n"
        //              + $"当天是否涨：{closeUp}";
        // Debug.Log(msg);
        return result;
    }
    /// <summary>
    /// 仅预测收盘价和凶的情况
    /// </summary>
    /// <param name="stockCode"></param>
    /// <param name="transtion"></param>
    /// <param name="transitionLastDay"></param>
    /// <returns></returns>
    public static MeihuayiZhanStockResult ZhanWithStockTransition4(string stockCode, TransitionData transtion, TransitionData transitionLastDay)
    {
        var date = DateTime.Parse(transtion.d);
        MeihuayiResult openPredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);
        MeihuayiResult closePredict = ZhanWithTimeAndCode(date.Year,date.Month,date.Day,openHour,openMinute, stockCode);

        MeihuayiZhanStockResult result = new MeihuayiZhanStockResult();

        //开盘是否涨
        bool openUp = transtion.o >= transitionLastDay.c;
        //收盘是否涨
        bool closeUp = transtion.c >= transitionLastDay.c;

        bool zhuGuaXiong = closePredict.zhuGua.jixiong == JiXiong.大凶 ;//|| closePredict.zhuGua.jixiong == JiXiong.凶;
        bool bianGuaZiong = closePredict.bianGua.jixiong == JiXiong.大凶 ;//|| closePredict.bianGua.jixiong == JiXiong.凶;
        if (zhuGuaXiong && bianGuaZiong)
        {
            result.predictXiong = 1f;
            if(!closeUp)
            {result.right = 1f;}//Debug.Log("收盘预测跌 正确");}
        }
        // string msg = $"当前交易数据：{JsonConvert.SerializeObject(transtion)}\n"
        //              + $"前一个交易日的数据：{JsonConvert.SerializeObject(transitionLastDay)}\n"
        //              + $"开盘预测：{JsonConvert.SerializeObject(openPredict)}\n"
        //              + $"收盘预测：{JsonConvert.SerializeObject(closePredict)}\n"
        //              + $"当天是否涨：{closeUp}";
        // Debug.Log(msg);
        return result;
    }
    public static MeihuayiResult ZhanWithTimeAndCode(int year,int  month,int  day,int hour, int min, string stockCode)
    {
        TimeUtility.SolarToLunar(year, month, day, hour, min,out int cyear, out int cmonth, out int cday, out int chour);

        int dizhi  = cyear % 12;
        if (dizhi == 0) dizhi = 12;
        
        //Debug.LogError($"{cyear}  取十二地支循环是：{dizhi}");
        
        int num1 = dizhi + cmonth + cday;
        int num2 = dizhi + cmonth + cday + chour;

        int num3 = num2;
        bool suc = int.TryParse(stockCode, out int stockNumber);
        if (suc)
            num3 = stockNumber;
        else
            Debug.LogError($"无法解析字符串为数字:{stockCode}");
        
        // string msg = 
        //     $"{year}年{month}月{day}日{hour}点{min}分"
        //     +"\n"
        //     +$"{TimeUtility.LunarYear2Name(cyear)}年{TimeUtility.LunarMonth2Name(cmonth)}{TimeUtility.LunarDay2Name(cday)} {TimeUtility.LunarHour2Name(chour)}时" 
        //     +"\n"+stockNumber;
        //
        // GameDebug.LogError(msg);

        var result = Meihuayi.ZhanBu(num1, num2, num3);

        //Debug.Log($"占卦结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}");

        return result;
    }
}